{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "88068885",
   "metadata": {},
   "source": [
    "# 脚本模式与解释器模式\n",
    "\n",
    "## 解释器模式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b32fe32b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello World!\")     # 注释，不会被执行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fda143df",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2                     # 赋值语句"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2aa4a6b",
   "metadata": {},
   "source": [
    "单独的变量值会被输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ed872180",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c08151c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "for idx in range(3):      # 多行代码第一行\n",
    "    print(idx)            # 多行代码第二行"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "72a318c1",
   "metadata": {},
   "source": [
    "## 脚本模式\n",
    "\n",
    "脚本模式需要将Python代码写入一个文本文件来运行。将上文解释器模式中使用的代码，写入一个文本文件，命名为“test.py”，其内容为：\n",
    "```python\n",
    "print(\"Hello World!\")                  # 一些注释，不会被执行\n",
    "a = 2\n",
    "a\n",
    "for idx in range(3):\n",
    "    print(idx)\n",
    "```\n",
    "\n",
    "使用魔术命令写入："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8ae46fa9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing test.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile test.py\n",
    "print(\"Hello World!\")                  # 一些注释，不会被执行\n",
    "a = 2\n",
    "a\n",
    "for idx in range(3):\n",
    "    print(idx)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2add7740",
   "metadata": {},
   "source": [
    "使用命令行执行："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3c9e5e78",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!\r\n",
      "0\r\n",
      "1\r\n",
      "2\r\n"
     ]
    }
   ],
   "source": [
    "!python test.py"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "b41c5807",
   "metadata": {},
   "source": [
    "## 区别\n",
    "\n",
    "- 解释器模式可以通过“Out:”的部分，输出代码最后一行的变量值；而脚本模式中，只有print()函数打印的内容会被显示到屏幕上，非print()函数的结果并不会被输出\n",
    "\n",
    "- 在解释器模式下，当输入代码有误时，解释器会给出错误原因，并前进到下一个输入，可以继续写入代码；而在脚本模式下，如果输入的代码包含错误，在错误之后的所有代码都不会被Python执行。\n",
    "\n",
    "## 适用范围\n",
    "\n",
    "解释器模式适合学习和调试代码，脚本模式适合处理现实中的实际任务。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87423970",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
